// -----------------------------------------------------------------------
//  <copyright file="DatabaseOptions.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2022 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://www.dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2022-11-07 21:03</last-date>
// -----------------------------------------------------------------------

using DaprPlus.EntityFrameworkCore.Properties;


namespace DaprPlus.EntityFrameworkCore;

/// <summary>
/// 数据库配置
/// </summary>
public class DatabaseOptions
{
    public const string Position = "Database";

    /// <summary>
    /// 获取或设置 迁移所在程序集，通常为API项目
    /// </summary>
    public string? MigrationsAssembly { get; set; }

    /// <summary>
    /// 获取或设置 主数据库连接串
    /// </summary>
    public string? ConnectionString { get; set; }

    /// <summary>
    /// 获取或设置 从数据库配置
    /// </summary>
    public SlaveDatabaseOptions[] Slaves { get; set; } = [];
}

/// <summary>
/// 从数据库连接配置
/// </summary>
public class SlaveDatabaseOptions
{
    private int _weight = 1;

    /// <summary>
    /// 获取或设置 名称
    /// </summary>
    public string? Name { get; set; }
    
    /// <summary>
    /// 获取或设置 连接字符串
    /// </summary>
    public string? ConnectionString { get; set; }

    /// <summary>
    /// 获取或设置 权重，范围[1,100]
    /// </summary>
    public int Weight
    {
        get => _weight;
        set
        {
            if (value is < 1 or > 100)
            {
                throw new ArgumentOutOfRangeException(ResEFCore.String_Set_Slave_Weight_OutOfRange);
            }

            _weight = value;
        }
    }
}
